fix: Handle LLM refusal responses to prevent NoneType errors#1799
Open
pk-zipstack wants to merge 1 commit intomainfrom
Open
fix: Handle LLM refusal responses to prevent NoneType errors#1799pk-zipstack wants to merge 1 commit intomainfrom
pk-zipstack wants to merge 1 commit intomainfrom
Conversation
When Anthropic (or other providers) refuse to generate a response due to safety restrictions, LiteLLM returns content=None with finish_reason="refusal". Previously, this caused a crash in _post_process_response when calling .find() on None, producing the unhelpful error: "'NoneType' object has no attribute 'find'". Now detect None content early in complete(), acomplete(), and stream_complete(), check finish_reason, and raise a descriptive LLMError that clearly indicates the refusal or empty response cause. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Test ResultsSummary
Runner Tests - Full Report
SDK1 Tests - Full Report
|
|
chandrasekharan-zipstack
approved these changes
Feb 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



What
stop_reason: "refusal") in SDK1'sLLM.complete(),acomplete(), andstream_complete()methods.LLMErrorinstead of crashing with'NoneType' object has no attribute 'find'.Why
content=Nonewithfinish_reason="refusal". The SDK did not check forNonecontent before passing it to_post_process_response(), which called.find()onNone."Error from LLM provider 'anthropic': 'NoneType' object has no attribute 'find'"— making it very hard to diagnose the actual issue (a safety refusal).How
llm.py—complete()andacomplete(): After extractingresponse_textfrom the LiteLLM response, check if it'sNone. If so, inspectfinish_reasonand raiseLLMErrorwith a clear message.llm.py—stream_complete(): Extracted chunk processing into_process_stream_chunk()helper. Checksfinish_reasonon each chunk for refusal.llm.py—_raise_for_empty_response(): New helper method that raisesLLMErrorwith status 400 for refusals, 500 for other empty responses.common.py—LLMResponseCompat: Hardened__str__()and__repr__()to handleNonetext without crashing.Before:
"Error from LLM provider 'anthropic': 'NoneType' object has no attribute 'find'"After:
"The LLM refused to generate a response due to safety restrictions. Please review your prompt and try again."Can this PR break any existing features. If yes, please list possible items. If no, please explain why. (PS: Admins do not merge the PR without this section filled)
Nonecontent before existing processing logic runs. Normal responses (wherecontentis a string) are completely unaffected — the new check is skipped. The only behavioral change is that refusal/empty responses now raise a descriptiveLLMErrorinstead of an opaqueAttributeError.Database Migrations
Env Config
Relevant Docs
stop_reason: "refusal""refusal"through asfinish_reasonRelated Issues or PRs
Dependencies Versions
Notes on Testing
content=None,finish_reason="refusal"), empty responses, and normal responses. All 13 new tests pass alongside the existing 70 SDK1 tests (83 total).Checklist
I have read and understood the Contribution Guidelines.